package gdut.jmxywz.action;

import gdut.jmxywz.been.BaseFunction;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.interceptor.AuthenticationInterceptor;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import net.sf.navigator.menu.MenuComponent;
import net.sf.navigator.menu.MenuRepository;

import org.apache.struts2.ServletActionContext;

public class MenuAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	public static final String TEACHER_SESSION_KEY = "teacherSessionKey";
	
	private BaseFunction baseFunction;
	
	private AuthenticationInterceptor authenticationInterceptor;

	public BaseFunction getBaseFunction() {
		return baseFunction;
	}

	public void setBaseFunction(BaseFunction baseFunction) {
		this.baseFunction = baseFunction;
	}

	public AuthenticationInterceptor getAuthenticationInterceptor() {
		return authenticationInterceptor;
	}

	public void setAuthenticationInterceptor(
			AuthenticationInterceptor authenticationInterceptor) {
		this.authenticationInterceptor = authenticationInterceptor;
	}

	public String execute() throws Exception {
		// TODO Auto-generated method stub
		HttpSession httpSession=ServletActionContext.getRequest().getSession();
		Teacher teacher=(Teacher)httpSession.getAttribute(TEACHER_SESSION_KEY);
		if(teacher==null){
			System.out.println("-----------------------没有教师----------------------");
		}
		
		MenuRepository repository=new MenuRepository();
		ServletContext application=(ServletContext)httpSession.getServletContext();
		MenuRepository defaultRepository=(MenuRepository)application.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
		
		repository.setDisplayers(defaultRepository.getDisplayers());
		
		String hql="select distinct rf.baseFunction from RoleFunction rf where rf.roles in " +
				"(select tr.roles from TeacherRole tr where tr.teacher.teacherId="+teacher.getTeacherId()+")"
				+"order by rf.baseFunction.displayOrder";
		
		List baseFunctionList=this.getCommonService().findListByHql(hql, null);
		
		if(baseFunctionList==null||baseFunctionList.isEmpty()){
			return "noPermit";
		}
        for (int i=0; i < baseFunctionList.size(); i++) {
			
			MenuComponent mc = new MenuComponent();
			BaseFunction baseFunction=(BaseFunction)baseFunctionList.get(i);
			if(baseFunction==null){
				System.out.println("====================baseFunction无值========================");
			}
			
			mc.setName(baseFunction.getFunctionName());
			mc.setTarget("mainFrame");
			
			List parentList=null;
			BaseFunction baseFunctionParent=null;
			if(baseFunction.getBaseFunction()!=null){
				String hqlFunctionParent="select bf from BaseFunction bf where bf.FunctionId="+baseFunction.getBaseFunction().getFunctionId();
				
				parentList=this.getCommonService().findListByHql(hqlFunctionParent, null);
				if(parentList==null||parentList.isEmpty()){
					return "error";
				}
				baseFunctionParent=(BaseFunction)parentList.get(0);
			}
			
			
			if (baseFunctionParent != null) {
				String parentName=baseFunctionParent.getFunctionName();
		        MenuComponent parentMenu = repository.getMenu(parentName);
		        if (parentMenu == null) {
		            parentMenu = new MenuComponent();
		            parentMenu.setName(parentName);
		            parentMenu.setTitle(parentName);
		            mc.setImage("images/foldericon.gif");
		            repository.addMenu(parentMenu);
		        }
				mc.setImage("images/openfoldericon.gif");
		        mc.setParent(parentMenu);
		    }else{
		    	mc.setImage("images/foldericon.gif");
		    }

			
			mc.setTitle(baseFunction.getFunctionName());
			mc.setLocation(baseFunction.getFunctionUrl());
			mc.setUrl(baseFunction.getFunctionUrl());
			repository.addMenu(mc);			
		}
		httpSession.setAttribute("repository", repository);
		
		return SUCCESS;
	}
	
	
}
